package weixintest;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2019/4/14 22:30
 */
public class MethodHandlerTest {
    private static MethodHandle getPrintlMH(Object receiver) throws NoSuchMethodException, IllegalAccessException {
        MethodType mt = MethodType.methodType(void.class, String.class);
        return MethodHandles.lookup().findVirtual(receiver.getClass(), "println", mt).bindTo(receiver);
    }

    public static void main(String[] args) throws Throwable {
        Object object = System.currentTimeMillis() % 2 == 0 ? System.out : new ClassA();
        getPrintlMH(object).invoke("icyfenxin");
    }

    static class ClassA {
        public void println(String s) {
            System.out.println("ClassA");
            System.out.println(s);
        }
    }
}
